package com.yssoft.service.core;

import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.yssoft.entity.core.*;
import com.yssoft.mapper.core.TAccountsMapper;
import com.yssoft.mapper.core.TEnterpriseLicenceMapper;
import com.yssoft.mapper.core.TEnterpriseMapper;
import com.yssoft.mapper.core.TPersonnelMapper;
import com.yssoft.mapper.core.TRolesMapper;
import com.yssoft.utils.ObjectConversion;
import com.yssoft.utils.UploadFile;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * @类名称: RegisterService
 * @类描述: 注册用service
 * @作者: 洪丰
 * @创建时间:2018年4月29日 上午10:03:46
 */
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
@Service
public class RegisterService
{
	@Autowired
	protected TRolesMapper rolesMapper;
	@Autowired
	protected TPersonnelMapper personnelMapper;
	@Resource
    private TAccountsMapper accountsMapper;
	@Resource
    private TEnterpriseMapper enterpriseMapper;
	@Resource
	private TEnterpriseLicenceMapper enterpriseLicenceMapper;
	
	/**
	 * @方法名称: queryAccountsByRecord
	 * @功能描述: 通过记录获取对应账号
	 * @作者: 洪丰
	 * @创建时间: 2018年4月29日 上午10:15:56
	 * @param record
	 * @return List<TAccounts>
	 */
	public Integer queryAccountsByRecord(TAccounts record){
		List<TAccounts> aList = accountsMapper.select(record);
		return aList.size();
	}
	/**
	 * @方法名称: queryPersonneByRecord
	 * @功能描述: 通过记录获取对应人员
	 * @作者: 洪丰
	 * @创建时间: 2018年4月29日 上午10:20:03
	 * @param record
	 * @return Integer
	 */
	public Integer queryPersonneByRecord(TPersonnel record){
		List<TPersonnel> pList = personnelMapper.select(record);
		return pList.size();
	}
	
	/**
	 * @方法名称: addAcc
	 * @功能描述: 新建账号
	 * @作者: 洪丰
	 * @创建时间: 2018年4月29日 上午10:42:05
	 * @param phone
	 * @param password
	 * @return String
	 */
    public Integer addAcc(String phone, String password){
    	
    	TPersonnel pEntity = new TPersonnel();
    	pEntity.setDeleteFlag("0");
    	pEntity.setPhone(phone);
    	pEntity.setCreateTime(new Date());
    	personnelMapper.insertPersonnelReturnId(pEntity);
    	
    	TAccounts aEntity = new TAccounts();
        String salt= System.currentTimeMillis()+pEntity.getId()+"";
        aEntity.setSalt(salt);//随机加密盐
        String newPassword = new SimpleHash("MD5", password, salt, 2).toString();
        aEntity.setPassword(newPassword);
        aEntity.setCreateTime(new Date());
        aEntity.settPId(pEntity.getId());
        aEntity.setLoginName(phone);
        aEntity.setDeleteFlag("0");
        accountsMapper.insertAccountsReturnId(aEntity);
        
        TRoles crole = new TRoles();
    	crole.setCode("entManager");
    	TRoles role = rolesMapper.selectOne(crole);
    	
    	Map<String,Object> param=new HashMap<>();
        param.put("list",Arrays.asList(role.getId()));
        param.put("accountId",aEntity.getId());
        accountsMapper.insertAccountAndRole(param);
        
        return  aEntity.getId();
    }
    
    /**
     * @方法名称: addEntAcc
     * @功能描述: 保存企业
     * @作者: 洪丰
     * @创建时间: 2018年4月29日 上午11:49:44
     * @param entity
     * @param accoutsId void
     */
    public Integer addEntAcc(TEnterprise entity, int accoutsId, HttpServletRequest request){
    	
    	entity.setCreateTime(new Date());
    	entity.setDeleteFlag("0");
    	enterpriseMapper.insertGetById(entity);
    	//预留许可证信息
    	if(null != entity.getCode() || null != entity.getLicenseImg()){
    		TEnterpriseLicence entLicence1 = new TEnterpriseLicence();
        	entLicence1.setCreateTime(new Date());
        	entLicence1.setDeleteFlag("0");
        	entLicence1.setTEId(entity.getId());
        	entLicence1.setType("1");
        	
        	enterpriseLicenceMapper.insert(entLicence1);
    	}
    	
    	//保存营业执照信息
    	if(null != entity.getCode() || null != entity.getLicenseImg()){
    		TEnterpriseLicence entLicence = new TEnterpriseLicence();
        	entLicence.setCreateTime(new Date());
        	entLicence.setDeleteFlag("0");
        	entLicence.setLicenceCode(entity.getCode());
        	entLicence.setTEId(entity.getId());
        	entLicence.setType("2");
        	entLicence.setLicenceImg(entity.getLicenseImg());
        	entLicence.setLicenceTimg(entity.getLicenseImgTh());
        	
        	enterpriseLicenceMapper.insert(entLicence);
    	}
    	
    	TAccounts aEntity = accountsMapper.selectByPrimaryKey(accoutsId);
    	TPersonnel pEntity = personnelMapper.selectByPrimaryKey(aEntity.gettPId());
    	
    	aEntity.settEId(entity.getId());
    	//关联企业和账号
    	accountsMapper.updateByPrimaryKeySelective(aEntity);
    	//关联企业和用户
    	pEntity.settEId(entity.getId());
    	personnelMapper.updateByPrimaryKeySelective(pEntity);
    	//当注册的时候session是空的,这个时候不用存储企业信息
        HttpSession session = request.getSession();
        Map<String, Object> sessionMap = (Map<String, Object>)session.getAttribute("appUser");;//将实体转换成map
        if(sessionMap!=null ){
            sessionMap.put("entId",  entity.getId());
            session.setAttribute("appUser", sessionMap);
        }
        return entity.getId();
    }
    
	public Map<String, Object> compressUploadedFile(MultipartFile mFile) {
		Map<String, Object> fileNames = UploadFile.compressUploadedFile(mFile);
		return fileNames;
	}
}
